package com.etime.yqtsponsor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.yqtsponsor.common.BaseMessage;
import com.etime.yqtsponsor.common.ListMessage;
import com.etime.yqtsponsor.common.PageMessage;
import com.etime.yqtsponsor.common.PojoMessage;
import com.etime.yqtsponsor.dto.VoteRegisterDto;
import com.etime.yqtsponsor.pojo.ActiveGroup;
import com.etime.yqtsponsor.pojo.ActiveTheme;
import com.etime.yqtsponsor.pojo.ActiveTimeLine;
import com.etime.yqtsponsor.pojo.UserBehavior;
import com.etime.yqtsponsor.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ly
 * @date 2022/7/19 14:53
 */
@Api(tags = "活动组管理的数据接口")
@RestController
@RequestMapping("/active/group")
public class ActiveGroupController {

    @Autowired
    private ActiveGroupService activeGroupService;

    @Autowired
    private ActiveThemeService activeThemeService;
    @Autowired
    private VoteRegisterService voteRegisterService;
    @Autowired
    private UserBehaviorService userBehaviorService;
    @Autowired
    private ActiveTimeLineService activeTimeLineService;

    @ApiOperation("删除活动组接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activeGroup", value = "活动组"),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户id")
    })
    @GetMapping("/delgroup")
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}, isolation = Isolation.DEFAULT)
    public BaseMessage deleteGroup(ActiveTheme activeTheme,ActiveGroup activeGroup, Integer currentUserId) {
        BaseMessage msg = new BaseMessage();
        //验证活动是否存在
        if (isActiveExist(activeGroup.getActiveId())) {
            //验证当前用户是否是活动的创建者
            if (isCreateUser(activeGroup.getActiveId(), currentUserId)) {
                //验证活动是否已经发布
                if (isActivePublish(activeGroup.getActiveId())) {
                    //验证活动是否有人报名了该活动组
                    if(isParticipate(activeGroup.getActiveId())){
                        //验证活动组和活动的ID是否匹配
                        if(activeTheme.getId() == activeGroup.getActiveId()){
                            Integer resault = activeGroupService.deleteById(activeGroup.getActiveId());
                            if(resault > 0){
                                msg.setMsg("删除成功");
                                msg.setCode(1);
                            }else {
                                msg.setMsg("删除失败");
                                msg.setCode(0);
                            }
                        }else {
                            msg.setMsg("活动组所在的活动不是当前活动，所以不能删除");
                        }
                    }else {
                        msg.setMsg("该活动组已经有人报名，无法删除");
                    }
                } else {
                    msg.setMsg("在活动时间内不允许删除活动");
                }
            } else {
                msg.setMsg("您不是活动的发起人，没有权限删除该活动");
            }
        } else {
            msg.setMsg("活动不存在请先创建活动");
        }
        return msg;
    }


    @ApiOperation("删除活动接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activeGroup", value = "活动组"),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户id")
    })
    @GetMapping("/delactive")
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}, isolation = Isolation.DEFAULT)
    public BaseMessage deleteActive(ActiveGroup activeGroup, Integer currentUserId) {
        BaseMessage msg = new BaseMessage();
        //验证是否是活动的创建者
        if (isCreateUser(activeGroup.getActiveId(), currentUserId)) {
            //验证当前时间是否是活动时间之外的时间，若是则可以删除
            if (isActiveStart(activeGroup.getActiveId())) {
                //删除活动
                int i = activeThemeService.deleteActive(activeGroup.getActiveId());
                //删除参与用户行为信息
                int j = userBehaviorService.deleteUserByActiveId(activeGroup.getActiveId());
                if(i>0 && j >0){
                    msg.setMsg("删除成功");
                    msg.setCode(1);
                }else {
                    msg.setMsg("删除失败");
                    msg.setCode(0);
                }
            } else {
                msg.setMsg("在活动时间内不允许删除活动");
            }
        } else {
            msg.setMsg("您不是活动的发起人，没有权限删除该活动");
        }
        return msg;
    }





    @ApiImplicitParam("id")
    @GetMapping("/text")
    @ApiOperation("测试接口")
    public void Test1(int id,Long pageNum, Long pageSize){
        Page<UserBehavior> userBehavior = userBehaviorService.selectBehaviorByActiveId(id,pageNum,pageSize);
        System.out.println(userBehavior.getTotal()+"xxxxxxxxxxxxxxxxxxxxx");
    }


    /**
     * 活动是否存在
     *
     * @return
     */
    public boolean isActiveExist(Integer id) {
        ActiveTheme activeTheme = activeThemeService.selectById(id);
        if (activeTheme != null && activeTheme.getDeleted() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 操作人是否为主办方
     *
     * @return
     */
    public boolean isCreateUser(Integer id, Integer createdUserId) {
        ActiveTheme activeTheme = activeThemeService.selectById(id);
        if (activeTheme != null && activeTheme.getCreatedUserId().equals(createdUserId)) {
            return true;
        }
        return false;
    }

    /**
     * 活动是否发布
     *
     * @return
     */
    public boolean isActivePublish(Integer id) {
        ActiveTheme activeTheme = activeThemeService.selectById(id);
        if (activeTheme != null && activeTheme.getPublished() == 1) {
            return true;
        }
        return false;
    }

    /**
     * 活动是否开始
     *
     * @return
     */
    public boolean isActiveStart(Integer id) {
        long nowDate = System.currentTimeMillis();
        ActiveTimeLine activeTimeLine = activeTimeLineService.selectById(id);
        if (nowDate >= activeTimeLine.getActiveStartLine()) {
            return true;
        }
        return false;
    }

    public boolean isParticipate(Integer id){
        Page<UserBehavior> userBehavior = userBehaviorService.selectBehaviorByActiveId(id,Long.parseLong("0"),Long.parseLong("10"));
        if(userBehavior.getTotal() == 0){
            return false;
        }
        return true;
    }

}
